//
//  ViewController.swift
//  WebsocketTest
//
//  Created by lantaiyuan on 2017/8/18.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var isConnect:Bool = false
    
        
    lazy var serverTF:UITextField = {
        let tf = UITextField()
        tf.borderStyle = .roundedRect
        tf.placeholder = "请输入ip地址"
        tf.text = "10.1.10.28"
        tf.clearButtonMode = .whileEditing
        return tf
    }()
    
    lazy var portTF:UITextField = {
        let tf = UITextField()
        tf.borderStyle = .roundedRect
        tf.placeholder = "请输入端口号"
        tf.text = "8099"
        tf.clearButtonMode = .whileEditing
        return tf
    }()
    
    lazy var msgTF:UITextField = {
        let tf = UITextField()
        tf.borderStyle = .roundedRect
        tf.placeholder = "请输入消息"
        tf.clearButtonMode = .whileEditing
        return tf
    }()
    
    lazy var userIdTF:UITextField = {
        let tf = UITextField()
        tf.borderStyle = .roundedRect
        tf.placeholder = "请输入userId"
        return tf
    }()
    
    lazy var openBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("连接Socket", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .red
        return btn
    }()
    
    lazy var sendBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("发送", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .red
        return btn
    }()
    
    lazy var closeBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("关闭Socket", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .red
        return btn
    }()
    
    lazy var contentTV:UITextView = {
       let tv = UITextView()
        tv.layer.borderWidth = 0.5
        tv.layer.borderColor = UIColor.blue.cgColor
        return tv
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(serverTF)
        view.addSubview(portTF)
        view.addSubview(msgTF)
        view.addSubview(userIdTF)
        view.addSubview(openBtn)
        view.addSubview(sendBtn)
        view.addSubview(closeBtn)
        view.addSubview(contentTV)
        
        openBtn.addTarget(self, action: #selector(openSocket), for: .touchUpInside)
        sendBtn.addTarget(self, action: #selector(sendMsg), for: .touchUpInside)
        closeBtn.addTarget(self, action: #selector(closeSocket), for: .touchUpInside)
        
        for v in view.subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        let viewDict = ["serverTF": serverTF,
                        "portTF": portTF,
                        "msgTF": msgTF,
                        "userIdTF":userIdTF,
                        "openBtn": openBtn,
                        "sendBtn": sendBtn,
                        "closeBtn": closeBtn,
                        "contentTV": contentTV] as [String : Any]
        
        let metrics = ["spacing":20]
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[serverTF]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[portTF]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[msgTF]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[userIdTF]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[openBtn]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[sendBtn]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[closeBtn]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-(spacing)-[contentTV]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        // V
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-(64)-[serverTF(40)]-(spacing)-[portTF(40)]-(spacing)-[msgTF(40)]-(spacing)-[userIdTF(40)]-(spacing)-[openBtn(40)]-(spacing)-[sendBtn(40)]-(spacing)-[closeBtn(40)]-(spacing)-[contentTV]-(spacing)-|",
            options: [],
            metrics: metrics,
            views: viewDict)
        )
        
        YMSocketSwift.shared.successClosure = {[weak self](msg:AnyObject) in
            self?.contentTV.text = msg as! String
        }
        
        YMSocketSwift.shared.failureClosure = {[weak self](error:String) in
            self?.contentTV.text = error
        }
        
        YMSocketSwift.shared.isConnectClosure = {[weak self](isConnect: Bool) in
            self?.isConnect = isConnect
        }
        
        YMSocketSwift.shared.closedSocket = {[weak self] in
           self?.contentTV.text = "Socket已关闭"
        }
    }
    
    func openSocket(){
        
        if (serverTF.text?.characters.count)! > 0 && (portTF.text?.characters.count)! > 0 {
            YMSocketSwift.shared.connect(server: serverTF.text, port: portTF.text, userId: userIdTF.text)
            
        }else{
            YMSocketSwift.shared.connect()
        }
    }
    
    func sendMsg(){

        if let msg = msgTF.text, isConnect {
            YMSocketSwift.shared.send(json: msg)
        }else{
            contentTV.text = "请重新连接Socket"
        }
    }
    
    func closeSocket(){
        YMSocketSwift.shared.close()
    }

}


